package cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterialdetail;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterialdetail.vo.PickTaskMaterialDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterialdetail.vo.PickTaskMaterialDetailRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.picktaskmaterialdetail.vo.PickTaskMaterialDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.scan.vo.ScanInfoReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktaskmaterialdetail.PickTaskMaterialDetailDO;
import cn.iocoder.yudao.module.hs.service.picktaskmaterialdetail.PickTaskMaterialDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 拣货物料明细")
@RestController
@RequestMapping("/hs/pick-task-material-detail")
@Validated
public class PickTaskMaterialDetailController {

    @Resource
    private PickTaskMaterialDetailService pickTaskMaterialDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建拣货物料明细")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:create')")
    public CommonResult<Long> createPickTaskMaterialDetail(@Valid @RequestBody PickTaskMaterialDetailSaveReqVO createReqVO) {
        return success(pickTaskMaterialDetailService.createPickTaskMaterialDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拣货物料明细")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:update')")
    public CommonResult<Boolean> updatePickTaskMaterialDetail(@Valid @RequestBody PickTaskMaterialDetailSaveReqVO updateReqVO) {
        pickTaskMaterialDetailService.updatePickTaskMaterialDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拣货物料明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:delete')")
    public CommonResult<Boolean> deletePickTaskMaterialDetail(@RequestParam("id") Long id) {
        pickTaskMaterialDetailService.deletePickTaskMaterialDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拣货物料明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:query')")
    public CommonResult<PickTaskMaterialDetailRespVO> getPickTaskMaterialDetail(@RequestParam("id") Long id) {
        PickTaskMaterialDetailDO pickTaskMaterialDetail = pickTaskMaterialDetailService.getPickTaskMaterialDetail(id);
        return success(BeanUtils.toBean(pickTaskMaterialDetail, PickTaskMaterialDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拣货物料明细分页")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:query')")
    public CommonResult<PageResult<PickTaskMaterialDetailRespVO>> getPickTaskMaterialDetailPage(@Valid PickTaskMaterialDetailPageReqVO pageReqVO) {
        PageResult<PickTaskMaterialDetailDO> pageResult = pickTaskMaterialDetailService.getPickTaskMaterialDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PickTaskMaterialDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拣货物料明细 Excel")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPickTaskMaterialDetailExcel(@Valid PickTaskMaterialDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PickTaskMaterialDetailDO> list = pickTaskMaterialDetailService.getPickTaskMaterialDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拣货物料明细.xls", "数据", PickTaskMaterialDetailRespVO.class,
                        BeanUtils.toBean(list, PickTaskMaterialDetailRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得待拣物料明细")
    @PreAuthorize("@ss.hasPermission('hs:pick-task-material-detail:query')")
    public CommonResult<List<PickTaskMaterialDetailRespVO>> getPickTaskMaterialDetail(@Valid ScanInfoReqVO scanInfoReqVO) {
        return success(pickTaskMaterialDetailService.getPickTaskMaterialDetailList(scanInfoReqVO));
    }

}